<?php include 'header.php' ?>

<div class="body">
<?php include 'rightMenu.php' ?>
    <div class="content">
        <div class="figure">
            <img src="images/jewelry.jpg" alt=""/>
        </div>
        <div class="products">
            <div class="paging">
                <div class="first">
                    <h2>Sản phẩm</h2>
                </div>
                <div>
                    <?php
                    include 'mySQLConnect.php';

                    //xac dinh bao nhieu dong
                    $display = 16;
                    // tinh tong so trang can hien thi
                    if (isset($_GET['page']) && (int)$_GET['page']) {
                        $page = $_GET['page'];

                    } else {
                        //neu chua xac dinh, thi tim so trang
                        $query = "SELECT COUNT(id) FROM product";
                        $res = mysql_query($query);
                        $rows = mysql_fetch_array($res) or die(mysql_error());
                        $record = $rows[0];
                        if ($record > $display) {
                            $page = ceil($record / $display);
                        } else {
                            $page = 1;
                        }
                    }

                    $start = (isset($_GET['start']) && (int)$_GET['start'] >= 0) ? $_GET['start'] : 0;

                    if ($page > 1) { //neu can hien thi so trang
                        echo '<ul>';
                        $next = $start + $display;
                        $prev = $start - $display;
                        $current = ($start / $display) + 1;

                        $datastrPrv = 'start=' . $prev . '&page=' . $page;
                        $datastrNext = "start=" . $next . "&page=" . $page;

                        //Hien thi trang Previous
                        if ($current != 1) {
                            ?>
                            <a href="product.php?<?php echo $datastrPrv; ?>">trước</a>
                        <?php
                        }
                        //Hien thi so link
                        for ($i = 1; $i <= $page; $i++) {
                            if ($current != $i) {
                                $datastrPage = "start=" . ($display * ($i - 1)) . "&page=" . $page;
                                ?>
                                <li><a href="product.php?<?php echo $datastrPage; ?>"><?php echo $i ?></a></li>
                            <?php
                            } else {
                                echo '<li class="currentpage"><a>' . $i . '</a></li>';
                            }
                        } //End: FOR

                        //Hien thi trang Next

                        if ($current != $page) {
                            ?>
                            <a href="product.php?<?php echo $datastrNext ?>">sau</a>
                        <?php
                        }

                        echo '</ul>';
                    }
                    ?>
                </div>
            </div>

            <?php
            $sql3 = "SELECT * FROM  product ORDER BY date DESC LIMIT $start, $display";

            $result3 = mysql_query($sql3);
            $count = 0;
            while ($row3 = mysql_fetch_array($result3)) {
                $count++;
                $image_url = $row3['image_url'];

                if (strpos($image_url, '../') !== false) {
                    $image_url = substr($image_url, 3);
                }

                $price = ($row3['price']);
                $temp = "";

                $price = strrev($price);
                while (strlen($price) >= 4) {
                    $temp = $temp . substr($price, 0, 3) . '.';
                    $price = substr($price, 3);
                }

                if (strlen($price) == 0) {
                    $price = strrev($temp);
                } else {
                    $price = strrev($price) . strrev($temp);
                }

                if ($count == 1) echo '<ul>';

                if ($count % 4 == 1) {
                    ?>
                    <li class="first">
                        <a href="detailProduct.php?id=<?php echo $row3['id'] ?>"><img src="<?php echo $image_url; ?>"
                                                                                      alt=""/></a>
                        <h4><?php echo urldecode($row3['name']) ?></h4>

                        <p><?php echo urldecode($row3['short_desc']) ?></p>
                        <span><?php echo $price ?> vnđ</span>
                    </li>
                <?php } else { ?>
                    <li>
                        <a href="detailProduct.php?id=<?php echo $row3['id'] ?>"><img src="<?php echo $image_url; ?>"
                                                                                      alt=""/></a>
                        <h4><?php echo urldecode($row3['name']) ?></h4>

                        <p><?php echo urldecode($row3['short_desc']) ?></p>
                        <span><?php echo $price ?> vnđ</span>
                    </li>
                <?php
                }
                if ($count % 4 == 0) {
                    echo '</ul><ul>';
                }
            }

            if ($count % 4 != 0) {
                echo '</ul>';
            }

            if ($count == 0) {
                echo "sản phẩm đang được cập nhật";
            }

            include 'mySQLClose.php';
            ?>
        </div>
    </div>

<?php include 'footer.php' ?>